using System;
using System.Collections.Generic;
using EyesoftheDragon.Components;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using XRPGLibrary;
using XRPGLibrary.TileEngine;
using XRPGLibrary.SpriteClasses;

namespace EyesoftheDragon.GameScreens
{
    public class GamePlayScreen :BaseGameState
    {
        Engine engine = new Engine(32, 32);
        private Tileset tileset;
        private TileMap map;
        private Player player;
        private AnimatedSprite sprite;

        public GamePlayScreen(Game game, GameStateManager manager)
            :base(game, manager)
        {
            player = new Player(game);
        }

        public override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            Texture2D spriteSheet = Game.Content.Load<Texture2D>(@"PlayerSprites\"
+ GameRef.CharacterGeneratorScreen.SelectedGender + GameRef.CharacterGeneratorScreen.SelectedClass);
            Dictionary<AnimationKey, Animation> animations = new Dictionary<AnimationKey, Animation>();

            Animation animation = new Animation(3,32,32,0,0);
            animations.Add(AnimationKey.Down, animation);
            animation = new Animation(3,32,32,0,32);
            animations.Add(AnimationKey.Left, animation);
            animation = new Animation(3,32,32,0,64);
            animations.Add(AnimationKey.Right, animation);
            animation = new Animation(3,32,32,0,96);
            animations.Add(AnimationKey.Up, animation);

            sprite = new AnimatedSprite(spriteSheet, animations);

            base.LoadContent();

            Texture2D tilesetTexture = Game.Content.Load<Texture2D>(@"Tilesets\tileset1");
            Tileset tileset1 = new Tileset(tilesetTexture, 8,8,32,32);

            tilesetTexture = Game.Content.Load<Texture2D>(@"Tilesets\tileset2");
            Tileset tileset2 = new Tileset(tilesetTexture, 8,8,32,32);

            List<Tileset> tilesets = new List<Tileset>();
            tilesets.Add(tileset1);
            tilesets.Add(tileset2);

            MapLayer layer = new MapLayer(100, 100);

            for (int y = 0; y < layer.Height; y++ )
            {
                for (int x = 0; x < layer.Width; x++)
                {
                    Tile tile = new Tile(0,0);
                    layer.SetTile(x,y,tile);
                }
            }

            var splatter = new MapLayer(100, 100);
            var random = new Random();

            for (var i = 0; i < 100; i++)
            {
                var x = random.Next(0, 100);
                var y = random.Next(0, 100);
                var index = random.Next(2, 14);

                Tile tile = new Tile(index, 0);
                splatter.SetTile(x, y, tile);
            }

            splatter.SetTile(1,0, new Tile(0,1));
            splatter.SetTile(2,0, new Tile(2,1));
            splatter.SetTile(3,0, new Tile(0,1));

            List<MapLayer> mapLayers = new List<MapLayer>();
            mapLayers.Add(layer);
            mapLayers.Add(splatter);

            map = new TileMap(tilesets, mapLayers);
        }

        public override void Update(GameTime gameTime)
        {
            player.Update(gameTime);
            sprite.Update(gameTime);

            if (InputHandler.KeyReleased(Keys.PageUp))
            {
                player.Camera.ZoomIn();
                if (player.Camera.Mode == CameraMode.Follow)
                    player.Camera.LockToSprite(sprite);
            }

            if (InputHandler.KeyReleased(Keys.PageDown))
            {
                player.Camera.ZoomOut();
                if (player.Camera.Mode == CameraMode.Follow)
                    player.Camera.LockToSprite(sprite);
            }

            Vector2 motion = new Vector2();

            if (InputHandler.KeyDown(Keys.W))
            {
                sprite.CurrentAnimation = AnimationKey.Up;
                motion.Y = -1;
            }
            else if (InputHandler.KeyDown(Keys.S))
            {
                sprite.CurrentAnimation = AnimationKey.Down;
                motion.Y = 1;
            }

            if (InputHandler.KeyDown(Keys.A))
            {
                sprite.CurrentAnimation = AnimationKey.Left;
                motion.X = -1;
            }

            if (InputHandler.KeyDown(Keys.D))
            {
                sprite.CurrentAnimation = AnimationKey.Right;
                motion.X = 1;
            }

            if (motion != Vector2.Zero)
            {
                sprite.IsAnimating = true;
                motion.Normalize();
                sprite.Position += motion * sprite.Speed;
                sprite.LockToMap();

                if (player.Camera.Mode == CameraMode.Follow)
                    player.Camera.LockToSprite(sprite);
            }
            else
                sprite.IsAnimating = false;

            if (InputHandler.KeyReleased(Keys.F))
            {
                player.Camera.ToggleCameraMode();
                if (player.Camera.Mode == CameraMode.Follow)
                    player.Camera.LockToSprite(sprite);
            }

            if (player.Camera.Mode != CameraMode.Follow)
            {
                if (InputHandler.KeyReleased(Keys.C))
                    player.Camera.LockToSprite(sprite);
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            GameRef.SpriteBatch.Begin(
                SpriteSortMode.Deferred,
                BlendState.AlphaBlend,
                SamplerState.PointClamp,
                null,
                null,
                null,
                player.Camera.Transformation
                );

            map.Draw(GameRef.SpriteBatch, player.Camera);
            sprite.Draw(gameTime, GameRef.SpriteBatch, player.Camera);
            base.Draw(gameTime);

            GameRef.SpriteBatch.End();
        }
    }
}